package com.jctrip.hotel.source.xxl.cnbe;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.constant.XxlJobNameConstant;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.domain.dao.HtSourceInfoDao;
import com.jctrip.hotel.domain.entity.HtSourceInfo;
import com.jctrip.hotel.source.model.CnBooking.IdentityInfoDTO;
import com.jctrip.hotel.source.model.CommonJobParam;
import com.jctrip.hotel.source.service.CNBooking.CNBookingTaskService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: zxy
 * @CreateTime: 2023-08-17 14:45
 * @Description:
 */
@Component
@Slf4j
public class SyncCNBEPartRatesTaskPopXxlJob extends IJobHandler {

    @Resource
    private CNBookingTaskService taskService;
    @Resource
    private HtSourceInfoDao htSourceInfoDao;

    @Override
    @XxlJob(XxlJobNameConstant.SYNC_CNBE_PART_RATES_POP)
    public void execute() {
        XxlJobHelper.log(XxlJobNameConstant.SYNC_CNBE_PART_RATES_POP + " xxl job start");
        try {
            CommonJobParam commonJobParam = JsonUtil.toObject(CommonJobParam.class, XxlJobHelper.getJobParam());
            HtSourceInfo htSourceInfo =
                    htSourceInfoDao.selectOne(new LambdaQueryWrapper<HtSourceInfo>().eq(HtSourceInfo::getSourceId,
                            commonJobParam.getSourceId()));
            if (htSourceInfo == null) {
                return;
            }
            IdentityInfoDTO infoDTO = IdentityInfoDTO.builder().appId(htSourceInfo.getSourceAppId())
                    .securityKey(htSourceInfo.getSourceSecret())
                    .userName((String) JsonUtil.toMap(htSourceInfo.getExtra()).get("userName"))
                    .passWord((String) JsonUtil.toMap(htSourceInfo.getExtra()).get("passWord"))
                    .signature((String) JsonUtil.toMap(htSourceInfo.getExtra()).get("signature"))
                    .days(commonJobParam.getDays()).build();
            taskService.syncCNBERatesTaskPop(infoDTO);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        XxlJobHelper.log(XxlJobNameConstant.SYNC_CNBE_PART_RATES_POP + " xxl job end");
    }
}